<template>
	<view class="page">
		<view class="safe" style="width: 100%; background-color: #FFCDD3" :style="{height:safe+'rpx'}"></view>
		<view class="top">
			<view class="tab">
				<!-- <text v-for="(item,index) in tabList" class="item" :key="index" :class="index == activeIndex ? 'active' : ''" @click="handelChange(index)">
					{{item.name}}
				</text> -->
				<image style="width: 200rpx; margin: 0 10rpx " src="../../static/mallLogo.jpg" mode="widthFix"></image>
				<view class="city" @click="enterCity">
					<text class="address">{{handelCity}}</text>
				</view>
				<u-icon style="margin-left: 120rpx;" name="search" color="#2979ff" size="38" @click="enterSearch"></u-icon>
			</view>
		</view>
		
		<view class="" style="padding:0 20rpx;">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="(item,index) in lunbolist" :key='index' @click="enterDetail(item)">
					<image style="width: 100%; height: 100%;" :src="item.picPosition[0]" mode="aspectFill"></image>
				</swiper-item>
				
			</swiper>
			<view class="hotbook">
				<moreT :titleName="'今日热门'" :type='1'></moreT>
				<goodsList :list='hotList'></goodsList>
				
			</view>
			<view class="hotType">
				<view class="moreT">
					<text class="title">热门分类</text>
					<view class="" @click="enterClass()">
						<text class="font12">全部分类</text>
						<u-icon name="arrow-right" color="#ccc" size="24"></u-icon>
					</view>
				</view>
				<view class="distirct">
					<view class="types" :class="{green:item.type==='green',pink:item.type==='pink',blue:item.type==='blue'}" v-for="(item,index) in typeList" :key='index' @click="enterMore(item)">
						<u-icon v-show="item.hot" name="star-fill" color="#ff0000" size="22"></u-icon>
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
			<!-- <view class="new">
				<moreT :titleName="'重磅新品'" :type='1'></moreT>
				<smallList :list='newList'></smallList>
			</view> -->
			<view class="hotRecomade">
				<view class="moreT">
					<text class="title">热门推荐</text>
					
				</view>
				<goodsList :list='recomadeList'></goodsList>
			</view>
			<view class="koubei">
				<moreT :titleName="'口碑'" :type='1'></moreT>
				<goodsList :list='greatList'></goodsList>
			</view>
			
			
			<view class="hotRecomade">
				<view class="moreT">
					<text class="title">猜你喜欢</text>
					<text class="font12"></text>
				</view>
				<goodsList :list='gussLikeList'></goodsList>
			</view>
		</view>
		
	</view>
</template>

<script>
	import goodsList from "../../component/goodsList.vue"
	import smallList from "../../component/smallBook.vue"
	import moreT from "../../component/open.vue"
	var QQMapWX = require('../../common/utils/qqmap-wx-jssdk.min.js');
	var qqmapsdk;
	export default {
		components: {
			goodsList,
			smallList,
			moreT
		},
		data() {
			return {
				// tabList:[{
				// 	name:'女生'
				// },{
				// 	name:'男生'
				// },{
				// 	name:'出版'
				// }],
				safe:this.globalData.safeH*2,
				authCity: '长沙市',
				latitude: '',
				longitude: '',
				chooseCity:'长沙市',
				activeIndex: 0,
				lunbolist:[],
				hotList: [],
				newList: [],
				greatList: [],
				recomadeList: [],
				gussLikeList: [],
				typeList: [{
					hot: true,
					name: '水果',
					type: 'pink'
				}, {
					hot: true,
					name: '零食',
					type: 'pink'
				}, {
					hot: false,
					name: '去火',
					type: 'pink'
				}, {
					hot: false,
					name: '热卖',
					type: 'green'
				}, {
					hot: true,
					name: '降价',
					type: 'blue'
				}, {
					hot: false,
					name: '喜欢',
					type: 'green'
				}, {
					hot: true,
					name: '干果',
					type: 'pink'
				}, {
					hot: false,
					name: '日用',
					type: 'pink'
				}]
			}
		},
		computed: {
			handelCity: function() {
				return this.chooseCity.substr(0, 2)
			}

		},
		onReady() {
			qqmapsdk = new QQMapWX({
				key: 'CYQBZ-QUVC4-KMGU5-XBARC-JZCVJ-K4FKW'
			})
		},
		onLoad() {
			let _this=this
			this.getUserLocation();
			let fCity = uni.getStorageSync('city')
			if(!fCity){
				this.chooseCity='长沙市'
				uni.setStorage({
					key:'city',
					data:this.chooseCity
				})
				
			}else{
				this.chooseCity=fCity
			}
			
		},
		onShow() {
			this.chooseCity= uni.getStorageSync('city')
			this.getData()
			
		},
		watch:{
			chooseCity:function(){
				
			}
		},
		methods: {
			getData(){
				let parmas={goodsMsg:null}
				this.$http.get('/goods_msg/allTypeList').then(res=>{
					console.log(res)
					this.hotList=res.data.hotList
					this.greatList=res.data.greatList
					this.newList=res.data.newList
					this.recomadeList=res.data.recomadeList
					this.gussLikeList=res.data.gussLikeList
					this.lunbolist=res.data.lunbolist
				})
			},
            enterDetail(it){
            	console.log("外层")
            	uni.navigateTo({
            		url:'../enter/detail?id='+it.goodsId
            	})
            },
			getUserLocation: function() {
				let vm = this;
				wx.getSetting({
					success: (res) => {
						console.log(JSON.stringify(res))
						// res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
						// res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
						// res.authSetting['scope.userLocation'] == true    表示 地理位置授权
						if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
							wx.showModal({
								title: '请求授权当前位置',
								content: '丹陆小店需要获取您的地理位置，请确认授权',
								success: function(res) {
									if (res.cancel) {
										wx.showToast({
											title: '拒绝授权',
											icon: 'none',
											duration: 1000
										})
									} else if (res.confirm) {
										wx.openSetting({
											success: function(dataAu) {
												if (dataAu.authSetting["scope.userLocation"] == true) {
													wx.showToast({
														title: '授权成功',
														icon: 'success',
														duration: 1000
													})
													//再次授权，调用wx.getLocation的API
													vm.getLocation();
												} else {
													wx.showToast({
														title: '授权失败',
														icon: 'none',
														duration: 1000
													})
												}
											}
										})
									}
								}
							})
						} else if (res.authSetting['scope.userLocation'] == undefined) {
							//调用wx.getLocation的API
							vm.getLocation();
						} else {
							//调用wx.getLocation的API
							vm.getLocation();
						}
					}
				})
			},
			// 微信获得经纬度
			getLocation: function() {
				let vm = this;
				wx.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log(JSON.stringify(res))
						var latitude = res.latitude
						var longitude = res.longitude
						var speed = res.speed
						var accuracy = res.accuracy;
						vm.getLocal(latitude, longitude)
					},
					fail: function(res) {
						console.log('fail' + JSON.stringify(res))
					}
				})
			},
			// 获取当前地理位置
			getLocal: function(latitude, longitude) {
				let _this = this;
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: latitude,
						longitude: longitude
					},
					success: function(res) {
						// console.log(JSON.stringify(res));
						let province = res.result.ad_info.province
						let city = res.result.ad_info.city
						_this.authCity = city
						let fitCity=uni.getStorageSync('city')
						if(_this.authCity!==fitCity&&fitCity){
							uni.showModal({
								title: '切换城市',
								content: '检测当前城市为'+_this.authCity+'是否切换城市',
								success: function(res) {
									if (res.cancel) {
										
									} else if (res.confirm) {
										_this.chooseCity=_this.authCity
										uni.setStorage({
											key:'city',
											data:_this.authCity
										})
									}
								}
							})
						}
						// uni.setStorage({
						// 	key:'loCity',
						// 	data:_this.authCity
						// })

					},
					fail: function(res) {
						console.log(res);
					},
					complete: function(res) {
						// console.log(res);
					}
				});
			},
			
			enterClass(){
				uni.switchTab({
					url:'./classify'
				})
			},
			handelChange(i) {
				this.activeIndex = i;
				uni.pageScrollTo({
				    scrollTop: 0,
				    duration: 200
				});
			},
			enterSearch(){
				uni.navigateTo({
					url:'../enter/search'
				})
			},
			enterMore(et){
				uni.navigateTo({
					url:'../enter/moreGoods?title=' + et.name
				})
			},
			enterCity() {
				uni.navigateTo({
					url: '../enter/city'
				})
			},
			

		}
	}
</script>



<style lang="scss" scoped>
	.page {
		width: 100%;
		height: 100%;
		background-color: #fff;
		

		swiper {
			width: 100%;
			height: 300rpx;
			margin-top: -150rpx;
			swiper-item {
				background-color: #19BE6B;
			}
		}
		.top{
			width: 100%;
			height: 250rpx;
			background-color: #FFCDD3;
		}
		.tab {
			background-color: #FFCDD3;
			width: 100%;
			height: 80rpx;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			// margin-bottom: 10rpx;
			
			
			.city{
				.address {
					margin-left: 10rpx;
					font-size: 32rpx;
					color: #333;
					position: relative;
					
				
					&::before {
						content: "";
						width: 0;
						height: 0;
						border-style: solid;
						border-width: 8rpx;
						border-color: #333 transparent transparent transparent;
						position: absolute;
						right: -25rpx;
						top: 50%;
						transform: translateY(-35%);
					}
				}
			}
			.item {
				margin-right: 30rpx;
				font-weight: bold;
				color: #ccc;
			}

			image {
				width: 50rpx;
				height: 50rpx;
				// background-color: #18B566;
				margin-left: 100rpx;
			}

			.active {
				position: relative;
				color: #ff0000;
				font-size: 36rpx;
				color: #000;

				&::after {
					content: "";
					position: absolute;
					bottom: -10rpx;
					left: 50%;
					transform: translateX(-50%);
					width: 40rpx;
					height: 4rpx;
					background-color: #ff0000;
					border-radius: 50rpx;
				}
			}
		}

		.moreT {
			width: 100%;
			height: 60rpx;
			margin: 20rpx 0;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.title {
				font-size: 30rpx;
				font-weight: bold;
			}

			.font12 {
				font-size: 22rpx;
				color: #999;
			}
		}


		.otherGoods,
		.newBooks {
			width: 100%;
			height: 240rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			flex-wrap: wrap;

			.items {
				height: 260rpx;
				width: 160rpx;
				line-height: 26rpx;

				image {
					height: 200rpx;
					width: 160rpx;
					background-color: #18B566;
				}

				.othername {
					font-size: 26rpx;

				}
			}
		}

		
		

		.hotType {
			width: 100%;
			height: 200rpx;

			.distirct{
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				flex-wrap: wrap;
				align-items: center;
				.types{
					width: auto;
					height: 36rpx;
					border-radius: 30rpx;
					padding: 0 20rpx;
					line-height: 34rpx;
					margin-right: 24rpx;
					margin-bottom: 20rpx;
				}
				.blue{
					background-color:rgba($color: #A0CFFF, $alpha: 0.2) ;
				}
				.pink{
					
					background-color:rgba($color: #FAB6B6, $alpha: 0.2) ;
				}
				.green{
					
					background-color:rgba($color: #71D5A1, $alpha: 0.2) ;
				}
			}

		}
	}
</style>
